package org.example.myleet.p1636;

import java.util.Arrays;

public class Solution {

    private static final int OFFSET = 100;

    public int[] frequencySort(int[] nums) {
        int n = nums.length;
        Integer[] numbers = new Integer[n];
        int[] buckets = new int[201];
        for (int i = 0; i < n; ++i) {
            int num = nums[i];
            ++buckets[num + OFFSET];
            numbers[i] = num + OFFSET;
        }
        Arrays.sort(numbers, (o1, o2) -> {
            if (buckets[o1] == buckets[o2]) {
                return o2 - o1;
            }
            return buckets[o1] - buckets[o2];
        });
        for (int i = 0; i < n; ++i) {
            nums[i] = numbers[i] - OFFSET;
        }
        return nums;
    }
}
